﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Taskbar;
using System.IO;
using Microsoft.WindowsAPICodePack.Shell;
using System.Reflection;
using System.Diagnostics;
using System.Xml;

namespace StartSubMenu
{
    public partial class MainForm : Form
    {
        private JumpList _jl = null;
        private string _pname = null;
        private string _xml = null;
        private string _link = null;

        //--------------------------------------------------------------------------------
        public MainForm()
        {
            InitializeComponent();

            Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);

            _pname = Process.GetCurrentProcess().ProcessName;
            this.Opacity = 0;
            _xml = _pname + ".xml";
            _link = _pname + ".lnk";
        }
        //--------------------------------------------------------------------------------
        private void MainForm_Shown(object sender, EventArgs e)
        {
            /*
             * command line - action
             * ---------------------
             * app name is "StartSubMenu.exe" - load Start Menu manager.
             * none - looking for {filename.ini}, load the orders, generate JumpList and exits.
             * /m - manage items.
             * */
            _jl = JumpList.CreateJumpList();

            if (!Path.GetDirectoryName(Application.ExecutablePath).ToLower().EndsWith("items"))
            {
                ShowStartMenuManager();
            }
            else if(Environment.CommandLine.EndsWith("/m"))
            {
                ShowItemsManager();
                RegisterManageItems();
            }
            else
            {
                if (File.Exists(_xml))
                {
                    // register the list
                    RegisterFromFile();
                }

                // register "manage items" task.
                RegisterManageItems();
            }
            
            _jl.Refresh();

            Application.Exit();
        }
        //--------------------------------------------------------------------------------
        private void RegisterManageItems()
        {
            this.AddJumpListLink(Application.ExecutablePath, "/m", "Manage Items...", Application.ExecutablePath);
        }
        //--------------------------------------------------------------------------------
        private void RegisterFromFile()
        {
            if (!File.Exists(_xml))
                return;

            XmlDocument doc = new XmlDocument();
            doc.Load(_xml);

            XmlNodeList items = doc.SelectNodes("//Item");

            Dictionary<string, Dictionary<string, string>> dictitems = new Dictionary<string, Dictionary<string, string>>();
            
            foreach(XmlNode item in items)
            {
                Dictionary<string, string> dictitem = new Dictionary<string, string>();
                dictitem.Add("path", item.Attributes["path"] == null ? null : item.Attributes["path"].InnerText);
                dictitem.Add("argument", item.Attributes["argument"] == null ? null : item.Attributes["argument"].InnerText);
                dictitem.Add("title", item.Attributes["title"] == null ? null : item.Attributes["title"].InnerText);
                dictitem.Add("icon", item.Attributes["icon"] == null ? null : item.Attributes["icon"].InnerText);
                
                if(dictitem["path"] == null)
                    throw new Exception("missing path attribute");

                dictitems.Add(dictitem["title"], dictitem);
            }
            
            Dictionary<string, Dictionary<string, string>>.Enumerator it = dictitems.GetEnumerator();

            while(it.MoveNext())
            {
                Dictionary<string, string> dictitem = it.Current.Value;
                this.AddJumpListLink(dictitem["path"], dictitem["argument"], dictitem["title"], dictitem["icon"]);
            }
        }
        //--------------------------------------------------------------------------------
        private void ShowItemsManager()
        {
            ItemsManagerForm im = new ItemsManagerForm(_xml);
            this.Text = im.Text;
            im.ShowDialog();
            
            this.RegisterFromFile();

            return;
        }
        //--------------------------------------------------------------------------------
        private void ShowStartMenuManager()
        {
            StartMenuManagerForm smm = new StartMenuManagerForm();
            smm.ShowDialog();
            return;
        }
        //--------------------------------------------------------------------------------
        private void AddJumpListLink(string path, string title)
        {
            this.AddJumpListLink(path, null, title, null);
        }
        //--------------------------------------------------------------------------------
        private void AddJumpListLink(string path, string arguments, string title)
        {
            this.AddJumpListLink(path, arguments, title, null);
        }
        //--------------------------------------------------------------------------------
        private void AddJumpListLink(string path, string arguments, string title, string icon)
        {
            string executing = path;

            JumpListLink item = new JumpListLink(executing, title == null ? path : title)
            {
                Arguments = arguments,
                IconReference = new IconReference(icon == null ? path : icon, 0)
            };
            

            _jl.AddUserTasks(item);
        }
        //--------------------------------------------------------------------------------
    }
}
